﻿using System.Collections.Generic;
using System.Data;
using System.Text;
using DocProcessingRepository.Interfaces;
using Entities;
using ServiceInterfaces;

namespace Services
{
    public class CsvFileService : ICsvFileService
    {
        private readonly IEnclosingJobRepository _enclosingJobRepository;
        private const int NumberOfColumnsForEnclosingJob = 5;
        public CsvFileService(IEnclosingJobRepository enclosingJobRepository)
        {
            _enclosingJobRepository = enclosingJobRepository;
        }

        public StringBuilder GetCSVReport(IList<JobEntity> jobs)
        {
            var csvBuilder = AddHeader();
            csvBuilder = AddJobRows(csvBuilder, jobs);
            return csvBuilder;

        }

        private StringBuilder AddHeader()
        {
            var csvBuilder = new StringBuilder();
            var headerColumns = GetHeaderColumns();
            foreach (var column in headerColumns)
            {
                csvBuilder.Append(column);
                csvBuilder.Append(",");
            }
            csvBuilder.Append("\n");
            return csvBuilder;
        }

        private StringBuilder AddJobRows(StringBuilder csvBuilder,IList<JobEntity> jobs)
        {
            foreach (var job in jobs)
            {
                csvBuilder.Append(job.JobReference);
                csvBuilder.Append(",");

                csvBuilder.Append(job.Company);
                csvBuilder.Append(",");

                csvBuilder.Append(job.Document);
                csvBuilder.Append(",");

                csvBuilder.Append(job.Version);
                csvBuilder.Append(",");

                csvBuilder.Append(job.Owner);
                csvBuilder.Append(",");

                csvBuilder.Append(job.SubmitDateTime);
                csvBuilder.Append(",");

                csvBuilder.Append(job.Status);
                csvBuilder.Append(",");

                csvBuilder.Append(job.Grid);
                csvBuilder.Append(",");

                csvBuilder.Append(job.ManCoCode);
                csvBuilder.Append(",");

                var enclosingJobs = _enclosingJobRepository.GetEnclosingJobsByJobId(job.JobId);

                if (enclosingJobs.Count > 0)
                {
                    AddRowsForEnclosingJobs(csvBuilder, enclosingJobs);
                }
                else
                {
                    csvBuilder.Append("\n");
                }
                
            }
            return csvBuilder;
        }

        private void AddRowsForEnclosingJobs(StringBuilder csvBuilder, IList<EnclosingJob> enclosingJobs)
        {
            for (int counter = 0; counter < enclosingJobs.Count; counter++)
            {
                var enclosingJob = enclosingJobs[counter];
                if (counter > 0)
                {
                    AddEmptyRowForJob(csvBuilder);
                }
                csvBuilder.Append(enclosingJob.Filename);
                csvBuilder.Append(",");

                csvBuilder.Append(enclosingJob.Pages);
                csvBuilder.Append(",");

                csvBuilder.Append(enclosingJob.Sheets);
                csvBuilder.Append(",");

                csvBuilder.Append(enclosingJob.Packs);
                csvBuilder.Append(",");

                csvBuilder.Append(enclosingJob.PostalDocketNumber);
                csvBuilder.Append(",");

                csvBuilder.Append("\n");
            }
        }

        private void AddEmptyRowForJob(StringBuilder csvBuilder)
        {
            var emptyColumns = GetHeaderColumns().Count - NumberOfColumnsForEnclosingJob;
            for (int counter = 0; counter < emptyColumns; counter++)
            {
                csvBuilder.Append(string.Empty);
                csvBuilder.Append(",");
            }
        }

        private IList<string> GetHeaderColumns()
        {
            return new List<string>
            {
                "JOB REFERENCE",
                "COMPANY",
                "DOCUMENT",
                "VERSION",
                "OWNER",
                "SUBMIT DATE/TIME",
                "STATUS",
                "GRID",
                "MANCO CODE",
                "FILENAME",
                "IMAGES",
                "SHEETS",
                "PACKS",
                "DOCKET NUMBER"
            };
        }



    }
}